package org.game.log.builder;

import org.game.log.dsp.Dsp;

/**
 * @author : DengYing
 * @CreateDate : 2017年12月18日 下午4:48:03
 * @Description ：Please describe this document
 */
public class LogDsp implements Cloneable{
	// DSP 分割符号
	public static final String C_COMMA = ",";
	public static final String C_COLON = ":";
	
	
//	// 来自DSP
//	public static final String FROM_DSP = "dsp";
	// 目标标签
	public static final String TARGET_TAG = "tag";
	
	private int dsp = 0;
	private StringBuilder sb = null;
	
	private int attrCount = 0;
	    
    private LogDsp(){
    	sb = new StringBuilder();
    }
    
    public static LogDsp newBuilder(Dsp dsp) {
    	LogDsp logDsp = new LogDsp();
//    	logDsp.addAttr(FROM_DSP, dsp.getCode());
    	logDsp.dsp = dsp.getCode();
        return logDsp;
    }
    
    public static LogDsp newBuilder(String content) {
    	LogDsp dsp = new LogDsp();
    	dsp.sb.append(content);
        return dsp;
    }
    
    
    @Override
	public LogDsp clone(){
    	LogDsp dsp = null;
		try {
			dsp = (LogDsp) super.clone();
			dsp.sb = new StringBuilder(sb);
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return dsp;
	}
	
	
	
	

    /** 目标标签 */
    public LogDsp targetTag(Object tag){
    	return addAttr(TARGET_TAG, tag);
    }
	
    
    public LogDsp addAttr(Object key,Object val){
    	if (attrCount > 0) {
    		sb.append(C_COMMA);
		}
    	sb.append(key).append(C_COLON).append(val);
    	attrCount++;
    	return this;
    }

	public LogDsp addAttr(Object key,Object[] val){
		if (attrCount > 0) {
			sb.append(C_COMMA);
		}
		String valStr = "";
		for (int i = 0; i < val.length; i++) {
			if (i>0){
				valStr+=C_COMMA;
			}
			valStr += val;
		}
		sb.append(key).append(C_COLON).append(valStr);
		attrCount++;
		return this;
	}
    
    // 添加过程
    public LogDsp addCourse(Object key){
    	if (attrCount > 0) {
    		sb.append(C_COMMA);
		}
    	sb.append(key).append(C_COLON).append(1);
    	attrCount++;
    	return this;
    }


    public int getDsp() {
		return dsp;
	}

	public String build(){
    	 String extText = sb.toString();
    	 sb.setLength(0);
         return extText;
    }
}
